using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using DTO;
using BUS;
using DAL;
using Utility;

public partial class Admin_loaisanphamlist : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadData();
        }
        if (Session["UserName"] == "")
        {
            Response.Redirect("dangnhap.aspx");
        }
    }
    private void LoadData()
    {
        Criteria cri = new Criteria();
        if (txt_tenloai.Text != "")
            cri.add(Condition.AND, tbloaispDTOColumn.Tenloaisp.ToString(), Condition.LIKE, txt_tenloai.Text);
        int total = 0;
        tbloaispDTOColection loaisanphamcollection =
            tbloaisanphamBUS.GET(cri.Criter, 0, 1, tbloaispDTOColumn.Maloaisp.ToString(), OrderDirection.ASC.ToString(), out total);
        Repeater1.DataSource = loaisanphamcollection;
        Repeater1.DataBind();
    }
    protected void btn_Timkiem_Click(object sender, EventArgs e)
    {
        LoadData();
    }
    protected void btn_xoa_Click(object sender, EventArgs e)
    {
        foreach (RepeaterItem item in this.Repeater1.Items)
        {
            CheckBox cb = (CheckBox)item.FindControl("cb_xoa");
            if (cb.Checked == true)
            {
                // xoa

                HiddenField hf_id = (HiddenField)item.FindControl("hf_id");
                tbloaisanphamBUS.Delete(int.Parse(hf_id.Value));
            }
        }
        LoadData();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem ||
           e.Item.ItemType == ListItemType.Item)
        {
            tbloaispDTO loai = (tbloaispDTO)e.Item.DataItem;

            HyperLink hyp_ten = (HyperLink)e.Item.FindControl("hyp_ten");
            HyperLink h = (HyperLink)e.Item.FindControl("hyp_Sua");

            Literal lit_ma = (Literal)e.Item.FindControl("lit_ma");

            HiddenField hf_id = (HiddenField)e.Item.FindControl("hf_id");
            hf_id.Value = loai.Maloaisp.ToString();
            lit_ma.Text = loai.Maloaisp.ToString();

            hyp_ten.NavigateUrl = "loaisanphamct.aspx?id=" + loai.Maloaisp.ToString();
            hyp_ten.Text = loai.Tenloaisp.ToString();

            HyperLink hyp_sua = (HyperLink)e.Item.FindControl("hpl_Edit");
            hyp_sua.NavigateUrl = "loaisanphamsua.aspx?id=" + loai.Maloaisp.ToString();
        }
    }
}
